using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Finance;

namespace EntLibEC.Dac.Finance
{
	/// <summary>
	/// Summary description for NetPayAlipayDac.
	/// </summary>
	public class NetPayAlipayDac
	{
		
		public NetPayAlipayDac()
		{
		}

		public int Insert(NetPayAlipayInfo oParam)
		{
			string sql = @"INSERT INTO Finance_NetPay_Alipay
                            (
                            SOSysNo, IsCustomerPay, CustomerPayTime
                            )
                            VALUES (
                            @SOSysNo, @IsCustomerPay, @CustomerPayTime
                            );set @SysNo = SCOPE_IDENTITY();";
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramSOSysNo = new SqlParameter("@SOSysNo", SqlDbType.Int,4);
			SqlParameter paramIsCustomerPay = new SqlParameter("@IsCustomerPay", SqlDbType.Int,4);
			SqlParameter paramCustomerPayTime = new SqlParameter("@CustomerPayTime", SqlDbType.DateTime);

			paramSysNo.Direction = ParameterDirection.Output;

			if ( oParam.SOSysNo != AppConst.IntNull)
				paramSOSysNo.Value = oParam.SOSysNo;
			else
				paramSOSysNo.Value = System.DBNull.Value;
			if ( oParam.IsCustomerPay != AppConst.IntNull)
				paramIsCustomerPay.Value = oParam.IsCustomerPay;
			else
				paramIsCustomerPay.Value = System.DBNull.Value;
			if ( oParam.CustomerPayTime != AppConst.DateTimeNull)
				paramCustomerPayTime.Value = oParam.CustomerPayTime;
			else
				paramCustomerPayTime.Value = System.DBNull.Value;
			
			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramSOSysNo);
			cmd.Parameters.Add(paramIsCustomerPay);
			cmd.Parameters.Add(paramCustomerPayTime);

			return SqlHelper.ExecuteNonQuery(cmd, out oParam.SysNo);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Finance_NetPay_Alipay SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sosysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int)
					{
						sb.Append(key).Append("=").Append(item.ToString());
					}
					else if ( item is decimal)
					{
						sb.Append(key).Append("=").Append( ((decimal)item).ToString(AppConst.DecimalFormat));
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SoSysNo=").Append(paramHash["SOSysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}
	}
}
